package com.example.administrator.activity.Homework.Homework6.Homework6_1;

import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.activity.R;

import java.util.HashMap;

public class Homework6Activity extends AppCompatActivity {
    /**
     * 在这个页面上增加2个按钮，一个是保存在SharedPreference，一个是保存在SD卡。
     * 当运行APP时，从SharedPreference中读取身高、体重数据并展示
     * @param savedInstanceState
     */
    EditText height,weight;
    Button SharedPreference;
    String filename="homework6.txt";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_homework6);
        init();
        String[] p={"android.permission.WRITE_EXTERNAL_STORAGE","android.permission.WRITE_EXTERNAL_STORAGE"};
        ActivityCompat.requestPermissions(Homework6Activity.this,p,1);
//         HashMap<String,String> hashMap=SpSaveHomework6.get(Homework6Activity.this);
        HashMap<String,String> hashMap=SDHomeWork6.get(Homework6Activity.this);
        height.setText(hashMap.get("ht"));
        weight.setText(hashMap.get("wt"));
        View.OnClickListener clickListener=new View.OnClickListener() {
            @Override
            public void onClick(View v) {

              String ht=height.getText().toString();
              String wt=weight.getText().toString();

                  if (TextUtils.isEmpty(ht)||TextUtils.isEmpty(wt)){
                      Toast.makeText(Homework6Activity.this, "不能空", Toast.LENGTH_SHORT).show();
                  }else {
                      boolean flag=SDHomeWork6.save(Homework6Activity.this,ht,wt);
//                      boolean flag=SpSaveHomework6.save(Homework6Activity.this,ht,wt);
                      if (flag){
                          Toast.makeText(Homework6Activity.this, "保存成功", Toast.LENGTH_SHORT).show();
                      }else {
                          Toast.makeText(Homework6Activity.this, "失败", Toast.LENGTH_SHORT).show();
                      }
                  }
            }
        };
        height.setOnClickListener(clickListener);
        weight.setOnClickListener(clickListener);
        SharedPreference.setOnClickListener(clickListener);

    }

    private void init() {
        height=findViewById(R.id.ch2_bmi_ed_height);
        weight=findViewById(R.id.ch2_bmi_ed_weight);
        SharedPreference=findViewById(R.id.SharedPreference);
    }
}
